package com.example.picture_sharing_system;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import utils.Picture;
import utils.PictureListAdapter;

public class SelectPictureActivity extends AppCompatActivity {

    private GridView gv_picture_list;
    private Cursor cursor;
    private List<Picture> mPictureList;

    private final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private PictureListAdapter pictureListAdapter;

    private String message;
    private int userid;


    private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (cursor != null && cursor.moveToPosition(position)) {
                String path = cursor.getString(cursor.getColumnIndex(
                        MediaStore.Images.Media.DATA
                ));
                Intent intent = new Intent(getApplicationContext(), PublishWorkActivity.class);
                intent.putExtra("picturePath", path);
                intent.putExtra("message", message);
                intent.putExtra("flag", 1);
                intent.putExtra("userid", userid);

                startActivity(intent);
                finish();
            }
        }
    };

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_picture);


        gv_picture_list = findViewById(R.id.gv_picture_list);

        Intent intent = getIntent();
        message = intent.getStringExtra("message");

        userid = intent.getIntExtra("userid", 0);

        pictureListAdapter = new PictureListAdapter(getApplicationContext());
        pictureListAdapter.swapCursor(null);
        gv_picture_list.setAdapter(pictureListAdapter);
        gv_picture_list.setOnItemClickListener(itemClickListener);




        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
            } else {
                requestPermissions(PERMISSIONS_STORAGE,
                        REQUEST_EXTERNAL_STORAGE);
            }
        } else {
            searchPicture();
        }


    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_EXTERNAL_STORAGE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    searchPicture();
                }
                break;
            default:
                break;
        }
    }

    public void searchPicture() {
        cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                null, null, null,
                MediaStore.Images.ImageColumns.DATE_ADDED + " DESC");


        pictureListAdapter.swapCursor(cursor);

    }

    @Override
    public void onBackPressed() {

        Intent intent = new Intent(getApplicationContext(), PublishWorkActivity.class);
        intent.putExtra("userid", userid);

        startActivity(intent);
        finish();
    }
}